[#include "/includes/_layout.html"/]
[@layout]
<div class="wrapper wrapper-content">
	<div class="row animated fadeInLeft">
	[#include "/includes/menu-product.html" /]
	<div class="col-sm-11">
	
	<div class="ibox float-e-margins">
		<div class="ibox-title row">
			<h2><small><a href="${webctx}/specification">《返回列表</a></small></h2>
		</div>
		<div class="row ibox-content active_statistics">
			<div class="title_query padtop"> 
				<form id="qiandaoForm" class="form-horizontal">
				  <div class="form-group">
			            <label class="col-md-2 control-label">规格名称<span style="color: red;"><em>*</em></span></label>
			            <div class="col-md-8" id="error_specification_name">
			            	<input type="hidden" id="specification_id" value="${specification.id}" class="input-xfat"  name="id"  >
			                <input id="specification_name" name="name" value="${specification.name }" maxlength="20" type="text" class="form-control" style="width: 200px" >
			                <label class="control-label" for="specification_name"></label>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-md-2 control-label">备注</label>
			            <div class="col-md-8" id="error_specification_memo">
			                <input id="specification_memo" name="memo" value="${specification.memo }" maxlength="20" type="text" class="form-control" style="width: 200px">
			                <label class="control-label" for="specification_memo"></label>
			            </div>
			        </div>
			        
			        <div class="form-group">
			            <label class="col-md-2 control-label">排序</label>
			            <div class="col-md-2" id="error_specification_orders">
			                <input id="specification_orders" name="orders" value="${specification.orders }" maxlength="20" type="text" class="form-control"  onkeyup="this.value=this.value.replace(/\D/g,'')">
			                <label class="control-label" for="specification_orders"></label>
			            </div>
			        </div>
			        
			         <div class="form-group">
			            <label class="col-md-2 control-label">添加规格值 <span style="color: red;"><em>*</em></span></label>
			            <div class="col-md-8" id="error_specification_value">
			                
			                 <div class="sign_activity sign_activity_tabin" id="sign_type_set_join">
		                        <ul class="sign_activity_list" id="prize_list_join">
		                        [#list specifitionValue as value]
		                        		<li [#if value.seller_id??] class="self" [#else] class="system" [/#if] item-id="${value.id}">
			                              	<p class="fl">
			                                  	规格值： <input placeholder="" value="${value.name}" class="input-xfat" style="width: 100px;" name="specificationValue_name"  type="text">
			                                  	排序：<input placeholder="" value="${value.orders}" class="input-xfat" style="width: 100px;" name="specificationValue_orders"  type="text" onkeyup="this.value=this.value.replace(/\D/g,'')"> 　
			                              	[#if value.seller_id??]
				                            	<a class="but_remove" href="javascript:void(0);" onclick="deleteSpecValue(this);">×</a>
				                            [/#if]
			                              	</p>
			                              	<input type="hidden" value="${value.id}" name="specificationValue_id"  >
			                            </li>
			                      [/#list]
		                        </ul>
		                        
		                        <textarea style="display:none;" id="html_model_join">                            
		                     	  <li class="self">
			                         <p class="fl">
		                               	规格值： <input placeholder="" class="input-xfat" style="width: 100px;" name="specificationValue_name"  type="text">
		                               	 排序：<input placeholder="" class="input-xfat" style="width: 100px;" name="specificationValue_orders"  type="text" onkeyup="this.value=this.value.replace(/\D/g,'')">　
				                      	<a class="but_remove" href="javascript:void(0);" onclick="deleteSpecValue(this);">×</a>
				                     </p>
				                     <input type="hidden" value="" name="specificationValue_id"  >
			                       </li>
			                    </textarea>
		                        <p>
		                            <a href="javascript:void(0);" onclick="addSpecValue('join');" class="btn-bordered btn-xlarge btn-primary">添加规格值</a>
		                            <label id="join_error_msg" style="color:red;"></label>
		                        </p>
			            </div>
			        </div>
			        </div>
				</form>
			</div>
		</div>
	</div>
	<div class="panel-footer" align="center">
	   	 <button id="saveSpeci" onclick="saveSpeci();" class="btn btn-sm btn-success"><i class="fa fa-dot-circle-o"></i>提交</button>
	</div>
	</div>
	</div>
</div>
<script type="text/javascript">
function addSpecValue(type){
	$("#prize_list_"+type).append($("#html_model_"+type).val());
}
function deleteSpecValue(obj){
	var itemId = $(obj).parent().parent().attr("item-id");
	if(itemId && itemId != ""){
		$(obj).parent().parent().hide();
		$(obj).parent().parent().attr("del","del");
	}else{
		$(obj).parent().parent().remove();
	}
}

function saveSpeci(){
	var params = {},error = {};
	var specificationName = $("#specification_name").val(), specificationMemo= $("#specification_memo").val(), 
	specificationOrders = $("#specification_orders").val(),specificationId= $("#specification_id").val();
	if(specificationId!=null && specificationId!="") params.id = specificationId;
	if(specificationName!=null && specificationName!="") params.name = specificationName;
	params.memo = specificationMemo;
	params.orders=specificationOrders;
	if($.trim(specificationName) =="") {error.error_specification_name = "规格名称不能为空"; } else {error.error_specification_name=""; params.name = $.trim(specificationName);}
	var hasError = false;
	  for(var key in error){
		if(error[key]!=""){
			if(!hasError) hasError = true;
			$("#"+key).addClass("has-error");
			$("#"+key).find("label").text(error[key]);
		}else{
			$("#"+key).removeClass("has-error");
			$("#"+key).find("label").empty();
		}
	}
	if(hasError) return false;
	
	var hasErr = false;
	$("#sign_type_set_join li.self").each(function(i,obj){
		var specificationValue_name = $(obj).find("input[name='specificationValue_name']");
		if(specificationValue_name.val()==""){
			if(!hasErr) hasErr=true;
			$("#join_error_msg").html("规格值不能为空");
			specificationValue_name.css("border-color", "#ff5454");
			return ;
		}
		var specificationValue_orders = $(obj).find("input[name='specificationValue_orders']");
		if(specificationValue_orders.val()==""){
			if(!hasErr) hasErr=true;
			$("#join_error_msg").html("排序不能为空");
			specificationValue_orders.css("border-color", "#ff5454");
			return ;
		}
	});
	if(hasErr){
		return false;
	}
	var items = new Array();
	$("#sign_type_set_join li.self").each(function(i, obj){
		var specificationValue_name= $(obj).find("input[name='specificationValue_name']").val();
		var specificationValue_orders= $(obj).find("input[name='specificationValue_orders']").val();
		var specificationValue_id= $(obj).find("input[name='specificationValue_id']").val();
		if(specificationValue_name!="" && specificationValue_orders!=""){
			var entity = new Object();
			entity.type = 1;
			entity.name = specificationValue_name;
			entity.orders=specificationValue_orders;
			if(specificationValue_id != ""){
				entity.itemId = specificationValue_id;
			}
			entity.del = $(obj).attr("del"); 
			items.push(entity);
		}
	});

	
	//排除del的规格值
	var itemsUnDels = new Array();
	for(var k=0;k<items.length;k++){
		var _objSpVaule = items[k];
		if(_objSpVaule.del != "del"){
			itemsUnDels.push(_objSpVaule);
		}
	}
	
	if(itemsUnDels.length <=0) {
		obz.warn("请填写规格值");
		return;
	}
	
	params.items =  JSON.stringify(items);
	$(".wrapper-content").mask("正在保存...");
	$.post(obz.ctx+"/specification/save", params, function(json) {
		$(".wrapper-content").unmask();
		if(json.code!=200){
			BootstrapDialog.alert({title:'提示', message:json.msg});
		}else{
			obz.msg("保存成功", function(){
				location.href=obz.ctx + "/specification/index";
			});
		}
	},"json");
}
</script>
[/@layout]
<script>
Template.init("#menu-goods");
Template.initSecond("#menu-speci-secd");
</script>